//
//  NSDate+SinaDate.swift
//  weibo
//
//  Created by chengqiang on 16/7/29.
//  Copyright © 2016年 chengqiangBiaodashi. All rights reserved.
//

import UIKit

extension NSDate {
    
    
    class func date(sinaDateString:String)->NSDate? {
        //将新浪返回的字符串转化成系统时间NSDate
        let df = NSDateFormatter()
        //设置要转换的时间格式
        df.dateFormat = "EE MMM dd HH:mm:ss zzz yyy"
        
        df.locale = NSLocale(localeIdentifier: "EN")
        
        let date = df.dateFromString(sinaDateString)
        return date
    }
    func sinaDateDescription() -> String {
        
        //获取当前时间
        let calendar = NSCalendar.currentCalendar()
        if calendar.isDateInToday(self)
        {
            let deta = Int(NSDate().timeIntervalSinceDate(self))
                if deta<60{
                return "刚刚"
            }else if deta < 60 * 60 {
                let min = deta/60
                return "\(min)分钟前"
                }else{
                    let h = deta/60/60
                    return "\(h)小时前"
            }
        }
        
        var dateFormat = ""
        if calendar.isDateInYesterday(self){
            dateFormat = "昨天 HH:mm"
        }else if calendar.compareDate(self, toDate: NSDate(), toUnitGranularity: NSCalendarUnit.Year)==NSComparisonResult.OrderedSame{
            dateFormat = "MM-dd HH:mm"
        }else{
            dateFormat = "yyy-MM-dd HH:mm"
        }
        let df = NSDateFormatter()
        df.dateFormat = dateFormat
        df.locale = NSLocale(localeIdentifier: "EN")
        let dateString = df.stringFromDate(self)
        return dateString
    }
}
